{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# <center>RegEx in Python</center>\n",
    "\n",
    "![](images/memes/meme7.jpg)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# The Backslash Plague\n",
    "\n",
    "Let's start with an example.\n",
    "\n",
    "Consider a text containing some Windows style directory addresses in which we have to find `C:\\Windows\\System32` substring."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import re"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "txt = \"\"\"\n",
    "C:\\Windows\n",
    "C:\\Python\n",
    "C:\\Windows\\System32\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "pattern = re.compile(\"C:\\Windows\\System32\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "pattern.search(txt)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Why are no matches found for above pattern?\n",
    "\n",
    "Regex Engine is treateing `\\` as metacharacters, whereas we intend to treat it like a literal.\n",
    "\n",
    "### Solution???\n",
    "\n",
    "We need to escape the metacharacters. A metacharacter can be escaped by putting a `\\` before it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "pattern = re.compile(\"C:\\\\Windows\\\\System32\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "pattern.search(txt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C:\\Windows\\System32\n"
     ]
    }
   ],
   "source": [
    "print(\"C:\\\\Windows\\\\System32\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Still no match found. Why???\n",
    "\n",
    "`\\` is used as an escape at two different levels. \n",
    "\n",
    "- First, the Python interpreter itself performs substitutions for `\\` before the `re` module ever sees the pattern string. For instance, `\\n` is converted to a newline character, `\\t` is converted to a tab character, etc. \n",
    "\n",
    "- Finally, `re` reads the substituted pattern string and will apply its own substitutions for `\\` character. \n",
    "\n",
    "Hence, to use `\\` as a **literal**, we first escape `\\` with `\\\\` for python interpreter and then escape `\\\\` as `\\\\\\\\` for regex engine."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "pattern = re.compile(\"C:\\\\\\\\Windows\\\\\\\\System32\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<_sre.SRE_Match object; span=(22, 41), match='C:\\\\Windows\\\\System32'>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pattern.search(txt)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Can we use 2 backslashes instead of 4 here?\n",
    "\n",
    "Yes. By using **raw-strings**, we do not need to put escapes at first level. \n",
    "\n",
    "> Python raw strings are represented as ***r\"your string\"***. In raw strings, no escaping is required as escape sequences like `\\n`, `\\t`, etc are not processed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "pattern = re.compile(r\"C:\\\\Windows\\\\System32\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<_sre.SRE_Match object; span=(22, 41), match='C:\\\\Windows\\\\System32'>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pattern.search(txt)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Do we really need to use 2 backslashes?\n",
    "\n",
    "If you are **not using any metacharacters** in your regex pattern, you can use `re.escape()` method to escape all the characters in pattern except ASCII letters, numbers and '_'."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'C\\\\:\\\\\\\\Windows\\\\\\\\System32'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "re.escape(\"C:\\Windows\\System32\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<_sre.SRE_Match object; span=(22, 41), match='C:\\\\Windows\\\\System32'>"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "re.search(re.escape(\"C:\\Windows\\System32\"), txt)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](images/memes/meme8.jpg)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
